<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增仓库存储详情')" />
    <style>
        .fa-search{
            position: relative;
            top: -25px;
            left: 10px;
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-mallStoreDetail-add">
        <div class="form-group hidden">
            <label class="col-sm-3 control-label">合同ID：</label>
            <div class="col-sm-8">
                <input name="contractId" th:value="1" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group hidden">
            <label class="col-sm-3 control-label">仓库ID：</label>
            <div class="col-sm-8">
                <input name="warehouseId" class="form-control" type="text" th:value="1">
            </div>
        </div>
        <div class="form-group hidden">
            <label class="col-sm-3 control-label">出库类型：</label>
            <div class="col-sm-8">
                <input name="type" th:value="1" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group hidden">
            <label class="col-sm-3 control-label">详情Id：</label>
            <div class="col-sm-8">
                <input name="orderDetailId" class="form-control" type="text" th:value="1">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">产品名称：</label>
            <div class="col-sm-8">
                <input id="productId" name="productId" class="form-control hidden" type="text" value="">
                <div class="input-group">
                    <input  type="text" class="form-control" id="suggest-demo-1" >
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-white dropdown-toggle" style="height: 31px" data-toggle="dropdown">
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">产品编码：</label>
            <div class="col-sm-8">
                <input name="code" class="form-control" readonly="readonly" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">价格：</label>
            <div class="col-sm-8">
                <input name="price" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">数量：</label>
            <div class="col-sm-8">
                <input name="num" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">合作伙伴：</label>
            <div class="col-sm-8">
                <input name="supplierId" class="form-control hidden" type="text">
                <input name="supplierName" class="form-control" style="padding-left: 28px" type="text" onclick="selectOwner()">
                <i class="fa fa-search"></i>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input name="remark" class="form-control" type="text">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-suggest-js" />
<th:block th:include="include :: bootstrap-typeahead-js" />
<script th:inline="javascript">
    var prefix = ctx + "warehouse/sellStoreDetail"
    $("#form-mallStoreDetail-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            let productId = $("#productId").val();
            debugger;
            $.ajax({
                url: ctx + "warehouse/warehouseStore/list",
                type: "post",
                data: {"productId": productId},
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        if(result.rows.length==0){
                            $.modal.alertError("仓库不存在该产品");
                        }else{
                          let numDB= result.rows[0].num;
                          let  num= $("input[name='num']").val();
                          if(isEmptyString(num)){
                              $.modal.alertError("数量不能为空");
                          }else{
                              if(numDB<num){
                                  $.modal.alertError("库存只有："+numDB);
                              }else{
                                  $.operate.save(prefix + "/add", $('#form-mallStoreDetail-add').serialize());
                              }
                          }
                        }
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
            //$.operate.save(prefix + "/add", $('#form-mallStoreDetail-add').serialize());
        }
    }



    $("#suggest-demo-1").bsSuggest({
        url: ctx + "product/product/selectMallStoreProductList?name=",
        allowNoKeyword: true,
        idField: "productId",
        keyField: "name",
        hideOnSelect: true,
        effectiveFields:['name','code','unit','price'],// 有效显示于列表中的字段，非有效字段都会过滤，默认全部有效。
        effectiveFieldsAlias: {"name":"产品名称","code":"产品编码","unit":"单位","price":"价格"},
        fnAdjustAjaxParam: function(keyword, opts) {
            //console.log("ajax 请求参数调整：", keyword, opts);
            opts.url = opts.url + keyword;
        },
    })
        .on("onDataRequestSuccess", function(e, result) {
            console.log("onDataRequestSuccess: ", result);
        })
        .on("onSetSelectValue", function(e, keyword, data) {
            //判断产品是否存在
            $.ajax({
                url: ctx + "warehouse/warehouseStore/list",
                type: "post",
                data: {"productId": keyword.id},
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        if(result.rows.length==0){
                            $.modal.alertError("仓库不存在该产品");
                        }else{
                            $("#productId").val(keyword.id);
                            $("input[name='code']").val(result.rows[0].code);
                            $("input[name='price']").val(result.rows[0].price);
                            debugger;
                        }
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        })
        .on("onUnsetSelectValue", function() {
            console.log("onUnsetSelectValue");
        });

    /*
    $("#suggest-demo-1").bsSuggest({
        url: ctx + "product/product/selectMallStoreProductList",
        idField: "productId",
        keyField: "name"
    }).on('onDataRequestSuccess', function (e, result) {
        console.log('onDataRequestSuccess: ', result);
    }).on('onSetSelectValue', function (e, keyword) {
        //判断产品是否存在
        $.ajax({
            url: ctx + "warehouse/warehouseStore/list",
            type: "post",
            data: {"productId": keyword.id},
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    if(result.rows.length==0){
                        $.modal.alertError("仓库不存在该产品");
                    }else{
                        $("#productId").val(keyword.id);
                        $("input[name='code']").val(result.rows[0].code);
                        $("input[name='price']").val(result.rows[0].price);
                        debugger;
                    }
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
        console.log('onSetSelectValue: ', keyword);
    }).on('onUnsetSelectValue', function (e) {
        $("#productId").val('');
        console.log("onUnsetSelectValue");
    });*/

    function selectOwner(){
        $.modal.open("选择合作伙伴",ctx + "partner/tenementInfo?type=1",1200,($(window).height() - 50),function (index, layero) {
            var rows = layero.find('iframe')[0].contentWindow.getSelectionRadio();
            if(rows.length == 0){
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }else{
                $("input[name='supplierId']").val(rows[0].deptId);
                $("input[name='supplierName']").val(rows[0].deptName);
                $.modal.close(index);
            }
        });
    }
</script>
</body>
</html>
